package edu.task.repository;

import edu.task.model.SysSubtask;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysSubtaskRepository extends JpaRepository<SysSubtask, Long> {

    @Modifying
    @Query(value = "delete ts from sys_task_subtask as ts " +
            "inner join sys_task as t on ts.task_id = t.task_id " +
            "inner join sys_subtask as s on s.subtask_id = ts.subtask_id " +
            "where ts.subtask_id=:subtaskId ", nativeQuery = true)
    void deleteTaskSubtaskBySubtaskId(Long subtaskId);

    @Modifying
    @Query(value = " delete from sys_subtask where subtask_id=:subtaskId ", nativeQuery = true)
    int deleteSubtaskById(Long subtaskId);

    @Query(value = "select * from sys_subtask as s " +
            "inner join sys_task_subtask as ts on ts.subtask_id = s.subtask_id " +
            "inner join sys_task as t on t.task_id = ts.task_id " +
            "where ts.task_id=:taskId", nativeQuery = true)
    List<SysSubtask> selectSubtaskListByTaskId(Long taskId);

}
